package com.project.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.server.common.api.Result;
import com.project.server.entity.HealthRecord;
import com.project.server.entity.PatientInfo;
import com.project.server.entity.SysUser;
import com.project.server.service.DoctorPatientService;
import com.project.server.vo.PatientVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/doctor/patients")
public class DoctorPatientController extends BaseController {

    @Autowired
    private DoctorPatientService doctorPatientService;

    @GetMapping
    public Result<Page<SysUser>> getPatients(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            HttpServletRequest request) {
        return Result.success(doctorPatientService.getPatients(getCurrentUserId(request), current, size, name));
    }

    @GetMapping("/{userId}/detail")
    public Result<PatientVO> getPatientDetail(@PathVariable Long userId) {
        return Result.success(doctorPatientService.getPatientDetail(userId));
    }

    @GetMapping("/{userId}/records")
    public Result<Page<HealthRecord>> getPatientRecords(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        return Result.success(doctorPatientService.getPatientRecords(userId, current, size));
    }

    @PostMapping("/{userId}/update")
    public Result<Void> bindPatient(
            @PathVariable Long userId,
            @RequestBody PatientInfo patientInfo,
            HttpServletRequest request) {
        doctorPatientService.update(getCurrentUserId(request), userId,patientInfo);
        return Result.success();
    }

    @PostMapping("/{userId}/unbind")
    public Result<Void> unbindPatient(
            @PathVariable Long userId,
            HttpServletRequest request) {
        doctorPatientService.unbindPatient(getCurrentUserId(request), userId);
        return Result.success();
    }
}
